﻿//***********************************************************************
// Assembly         : IGolovkine.AoA.Tcp.Channels.Http
// Author           : Ilia
// Created          : 06-11-2011
//
// Last Modified By : Ilia
// Last Modified On : 01-04-2010
// Description      : 
//
// Copyright        : (c) Microsoft. All rights reserved.
//***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace IGolovkine.AoA.Compression
{
    public class DeflateCompression : ICompressionAlgorithm
    {
        public const string Name = "Deflate";

        public byte[] Compress(byte[] uncompressedBuffer)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (DeflateStream gzip = new DeflateStream(ms, CompressionMode.Compress, true))
                {
                    gzip.Write(uncompressedBuffer, 0, uncompressedBuffer.Length);
                }
                byte[] compressedBuffer = ms.ToArray();
                return compressedBuffer;
            }
        }

        public byte[] Decompress(byte[] compressedBuffer)
        {
            using (DeflateStream gzip = new DeflateStream(new MemoryStream(compressedBuffer), CompressionMode.Decompress))
            {
                byte[] uncompressedBuffer = gzip.ReadAllBytes(compressedBuffer.Length);
                return uncompressedBuffer;
            }
        }

        string ICompressionAlgorithm.Name
        {
            get { return DeflateCompression.Name; }
        }

    }
}
